//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.utils.indexer;

import java.io.*;
import helliker.id3.*;
import ants.p2p.utils.donkey.*;
import ants.p2p.utils.encoding.*;

public class FileInfos implements Serializable{
  String hash;
  String ed2kHash;
  Object[] chunckHashes;
  long lastModified;
  String name;
  long size;
  MP3File mp3File = null;
  String comment = "";

  public FileInfos(File f) throws Exception{
    if(!f.exists())
      throw new IOException("File not exists.");
    this.name=f.getAbsolutePath();
    DigestManager dm = new DigestManager();
    this.chunckHashes = dm.getDigests(f);
    this.hash = dm.getDigest(this.chunckHashes);
    this.ed2kHash = Base16.toHexString(DonkeyHashFile.doHash(f)[0]);
    this.lastModified = f.lastModified();
    this.size = f.length();
    try{
      this.mp3File = new MP3File(this.name);
      if(!this.mp3File.isMP3())
        this.mp3File = null;
    }catch(Exception e){}
  }

  public long getLastModified(){
    return this.lastModified;
  }

  public String getName(){
    return this.name;
  }

  public String getHash(){
    return this.hash;
  }

  public String getED2KHash() {
    return this.ed2kHash;
  }

  public Object[] getChunckHashes(){
    return this.chunckHashes;
  }

  public long getSize() {
    return this.size;
  }

  public String getComment() {
    return this.comment;
  }

  public void setComment(String comment){
    this.comment = comment;
  }

  public boolean equals(Object o){
    if(o instanceof FileInfos)
      return ((FileInfos)o).hash.equals(this.hash);
    else if(o instanceof String)
      return ((String)o).equals(this.hash);
    else
      return this==o;
  }

  public boolean isMP3(){
    try{
      return mp3File.isMP3();
    }catch(Exception e){
      return false;
    }
  }

  public int getBitrate() throws Exception{
    return mp3File.getBitRate();
  }

  public String getAlbum() throws Exception {
    return mp3File.getAlbum();
  }

  public String getArtist() throws Exception {
    return mp3File.getArtist();
  }

  public String getYear() throws Exception{
    return mp3File.getYear();
  }

  public String getPlayingTimeString() throws Exception{
    return mp3File.getPlayingTimeString();
  }

  public String getExtendedInfos(){
    try {
      return "[MP3 Bitrate: " + this.getBitrate() + " - Length: " +
          this.getPlayingTimeString() + " - Artist: " +
          this.getArtist() + " - Album: " + this.getAlbum() + " - Year: " +
          this.getYear() + "]";
    }
    catch (Exception e) {
      return "";
    }
  }

  public String sizeToString(double dSize){
    double currentSize = dSize;
    int x = 1;
    dSize = dSize / Math.pow(2, 20);
    while (dSize > 1 && x < 5) {
      dSize = dSize / Math.pow(2, 10);
      x++;
    }
    String suffix = " bytes";
    String size = currentSize+suffix;
    if (x == 2) {
      suffix = " Mib";
      size = (new Double(currentSize * 1.0 / Math.pow(2, 20))).
          toString();
      int index = size.indexOf(".");
      if (index >= 0)
        size = size.substring(0, index + 2);
      size += suffix;
    }
    if (x == 3) {
      suffix = " Gib";
      size = (new Double(currentSize * 1.0 / Math.pow(2, 30))).
          toString();
      int index = size.indexOf(".");
      if (index >= 0)
        size = size.substring(0, index + 2);
      size += suffix;
    }
    if (x == 4) {
      suffix = " Tib";
      size = (new Double(currentSize * 1.0 / Math.pow(2, 40))).
          toString();
      int index = size.indexOf(".");
      if (index >= 0)
        size = size.substring(0, index + 2);
      size += suffix;
    }
    return size;
  }

  public String toString(){
    String result = this.getHash() + "  ";
    String size = this.sizeToString(this.getSize());
    result += " " +ji.JI.i("File Size")+": " + size;
    while (result.length() < 65)
      result += " ";
    result += " " + this.getName();
    result += " " + this.getExtendedInfos();
    return result;
  }
}
